﻿using System.Xml;
using com.woodsoft.core.Files;

namespace com.woodsoft.core {
  public static class Extensions {

    public static XmlAttribute GetAttribute ( this UIElement par , string name , string value ) {
      foreach( XmlAttribute attr in par.Attributes ) {
        if( attr.Name == name ) {
          return attr;
        }
      }
      return null;
    }
    public static void AddAttribute ( this UIElement par , string name , string value ) {
      par.RemoveAttribute( name );
      XmlAttribute attr = par.OwnerDocument.CreateAttribute( name );
      if( !string.IsNullOrEmpty( value ) ) {
        attr.Value = value;
      }
      par.Attributes.Append( attr );
    }
    public static void RemoveAttribute ( this UIElement par , string name ) {
      foreach( XmlAttribute attr in par.Attributes ) {
        if( attr.Name == name ) {
          par.Attributes.Remove( attr );
        }
      }
    }

    public static XmlElement GetElement ( this UIElement par , string name ) {
      XmlNodeList lst = par.ChildNodes;

      foreach( XmlNode nd in lst ) {
        if( nd.Name == name ) {
          return ( XmlElement ) nd;
        }
      }

      return null;
    }
    public static void AddElement ( this UIElement par , string name , string value ) {
      XmlElement nd = par.OwnerDocument.CreateElement( name );
      if( !string.IsNullOrEmpty( value ) ) {
        nd.InnerText = value;
      }
      par.AppendChild( nd );
    }
    public static void RemoveElement ( this UIElement par , string name ) {
      foreach( XmlNode nd in par.ChildNodes ) {
        if( nd.Name == name ) {
          par.RemoveChild( nd );
        }
      }
    }
    public static void RemoveAllElements ( this UIElement par ) {
      foreach( XmlNode nd in par.ChildNodes ) {
        par.RemoveChild( nd );
      }
    }

  }
}
